import { BaseWindow } from "../../../component/window/BaseWindow";
import { ILayer } from "../../../mvc/interfaces/ILayer";
import { Language } from "../../../../extend/language/Language";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { GameProxy } from "../../mvc/GameProxy";
import { Cache } from "../../cache/Cache";
import { MoneyUtil } from "../common/util/MoneyUtil";
import { TextBox } from "../pack/TextBox";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { TweenLite } from "../../../../com/greensock/TweenLite";
import { Cubic } from "../../../../com/greensock/easing/Cubic";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
type int = number;
//class MoneyAccessViewer
    
    export  class MoneyAccessViewer extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            titleHeight = 26;
            setSize(251, 182);
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            this.hintTextFlag = Language.getString(40098);
            var loc1=ObjCreate.createBg(18, 38, 215, 134, this, ImagesConst.WindowCenterB);
            ObjCreate.createBitmap(ImagesConst.Yuanbao, 45, 52, this);
            this._goldTextBox = this.createTextBox(67, 50, true, "gold");
            var loc2=ObjCreate.createGButton(Language.getString(40049), 176, 50, 43, 20, this);
            loc2.addEventListener(flash.events.MouseEvent.CLICK, this.goldHander);
            ObjCreate.createBitmap(ImagesConst.Tongqian, 45, 90, this);
            this._coinTextBox = this.createTextBox(67, 88, true, "coin");
            var loc3=ObjCreate.createGButton(Language.getString(40049), 176, 88, 43, 20, this);
            loc3.addEventListener(flash.events.MouseEvent.CLICK, this.coinHander);
            ObjCreate.createBg(18, 123, 215, 1, this, ImagesConst.SplitLine);
            this._texHint = ObjCreate.createTextField("", 30, 130, 120, 18, this);
            ObjCreate.createBitmap(ImagesConst.Yuanbao, 45, 150, this);
            this._curGoldNumTxt = ObjCreate.createTextField("", 75, 150, 360, 20, this);
            ObjCreate.createBitmap(ImagesConst.Tongqian, 140, 150, this);
            this._curCoinNumTxt = ObjCreate.createTextField("", 160, 150, 360, 20, this);
            return;
        }

        /* internal  */goldHander(arg1: Object /* flash.events.Event */): void
        {
            var loc1=this._goldTextBox._text.text.replace(new RegExp("\\,", "g"), "");
            if (loc1.length == 0 || loc1 == this.hintTextFlag) 
            {
                return;
            }
            GameProxy.roleProxy.changeMoney(this._isToSave, true, int(loc1));
            this._goldTextBox.label = this.hintTextFlag;
            return;
        }

        /* internal  */coinHander(arg1: Object /* flash.events.Event */): void
        {
            var loc1=this._coinTextBox._text.text.replace(new RegExp("\\,", "g"), "");
            if (loc1.length == 0 || loc1 == this.hintTextFlag) 
            {
                return;
            }
            GameProxy.roleProxy.changeMoney(this._isToSave, false, int(loc1));
            this._coinTextBox.label = this.hintTextFlag;
            return;
        }

        public updateData(): void
        {
            var loc1=Cache.instance.role.money;
            if (this._isToSave) 
            {
                this._curGoldNumTxt.htmlText = MoneyUtil.getGoldHtml(loc1.gold);
                this._curCoinNumTxt.htmlText = MoneyUtil.getGoldHtml(loc1.coin);
            }
            else 
            {
                this._curGoldNumTxt.htmlText = MoneyUtil.getGoldHtml(loc1.warehouseGold);
                this._curCoinNumTxt.htmlText = MoneyUtil.getGoldHtml(loc1.warehouseCoin);
            }
            return;
        }

        /* internal  */createTextBox(arg1: int, arg2: int, arg3: Boolean, arg4: string): TextBox
        {
            var loc1=null;
            (loc1 = new TextBox()).textFieldHeight = 20;
            loc1.textFieldWidth = 100;
            loc1.bgHeight = 20;
            loc1.bgWidth = 100;
            loc1.x = arg1;
            loc1.y = arg2;
            var loc2;
            (loc2 = loc1._text).defaultTextFormat = GlobalStyle.textFormat6;
            loc2.type = flash.text.TextFieldType.INPUT;
            loc2.y = 2;
            if (arg3) 
            {
                loc2.addEventListener(flash.events.Event.CHANGE, this.inputHander);
                loc2.restrict = "0-9";
                loc2.addEventListener(flash.events.FocusEvent.FOCUS_IN, this.focusChangeHander);
                loc2.addEventListener(flash.events.FocusEvent.FOCUS_OUT, this.focusChangeHander);
            }
            loc2.name = arg4;
            loc2.text = this.hintTextFlag;
            this.addChild(loc1);
            return loc1;
        }

        /* internal  */focusChangeHander(arg1: Object /* flash.events.FocusEvent */): void
        {
            var loc1=arg1.currentTarget as Object /* flash.text.TextField */;
            if (arg1.type == flash.events.FocusEvent.FOCUS_IN && loc1.text == this.hintTextFlag) 
            {
                loc1.text = "";
            }
            if (arg1.type == flash.events.FocusEvent.FOCUS_OUT && loc1.text == "") 
            {
                loc1.text = this.hintTextFlag;
            }
            return;
        }

        /* internal  */inputHander(arg1: Object /* flash.events.Event */): void
        {
            var loc3=0;
            var loc1=arg1.currentTarget as Object /* flash.text.TextField */;
            var loc2=loc1.text.replace(new RegExp("\\,", "g"), "");
            if (loc2.length == 0 || loc2 == "0") 
            {
                loc1.text = "";
            }
            else 
            {
                loc3 = int(loc2);
                loc3 = this.getAvaliableNum(loc3, loc1.name == "gold");
                loc1.text = MoneyUtil.getFormatInt(loc3);
                loc1.setSelection(loc1.text.length, loc1.text.length);
            }
            return;
        }

        /* internal  */getAvaliableNum(arg1: number, arg2: Boolean): number
        {
            var loc1=NaN;
            if (this._isToSave) 
            {
                loc1 = arg2 ? Cache.instance.role.money.gold : Cache.instance.role.money.coin;
            }
            else 
            {
                loc1 = arg2 ? Cache.instance.role.money.warehouseGold : Cache.instance.role.money.warehouseCoin;
            }
            if (arg1 > loc1) 
            {
                arg1 = loc1;
            }
            return arg1;
        }

        public reInit(arg1: Boolean): void
        {
            var loc1=this._isToSave;
            this._isToSave = arg1;
            if (arg1) 
            {
                this.reInitToSave();
            }
            else 
            {
                this.reInitToGet();
            }
            var loc2;
            this._coinTextBox.label = loc2 = this.hintTextFlag;
            this._goldTextBox.label = loc2;
            if (this.tweenObj) 
            {
                this.tweenObj.pause();
                this.tweenObj.kill();
            }
            if (this.parent) 
            {
                if (loc1 != arg1) 
                {
                    this.tweenObj = new TweenLite(this, 0.2, {"alpha": 0, "ease": Cubic.easeOut, "onComplete": this.show});
                }
                else 
                {
                    this.tweenObj = new TweenLite(this, 0.2, {"alpha": 0, "ease": Cubic.easeOut, "onComplete": hide});
                }
                this.tweenObj.play();
            }
            else 
            {
                this.show();
            }
            return;
        }

        /* internal  */reInitToGet(): void
        {
            title = Language.getString(40099);
            var loc1=HTMLUtil.addColor(Language.getString(40029), GlobalStyle.color6);
            this._texHint.htmlText = Language.getStringByParam(40101, loc1);
            this.updateData();
            return;
        }

        /* internal  */reInitToSave(): void
        {
            title = Language.getString(40100);
            var loc1=HTMLUtil.addColor(Language.getString(40028), GlobalStyle.color6);
            this._texHint.htmlText = Language.getStringByParam(40101, loc1);
            this.updateData();
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(278, 148);
            this.alpha = 0;
            this.tweenObj = new TweenLite(this, 0.5, {"alpha": 1, "ease": Cubic.easeOut});
            this.tweenObj.play();
            return;
        }

        /* internal */ /* var */tweenObj: TweenLite;

        /* internal */ /* var */_isToSave: Boolean;

        /* internal */ /* var */_goldTextBox: TextBox;

        /* internal */ /* var */_coinTextBox: TextBox;

        /* internal */ /* var */_texHint: Object /* flash.text.TextField */;

        /* internal */ /* var */_curGoldNumTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_curCoinNumTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */hintTextFlag: string;
    }
